package file

import (
	"fmt"
	"l_net/erro"
	"time"

	"l_net/pkg/server"
	"net"
	"path/filepath"
	"strings"

	"l_net/pkg/msg"

	mapset "github.com/deckarep/golang-set"
)

// 获取文件夹名和共享文件夹中间的文件路径，其中dir为 相对/绝对 路径、fileAbsPath为 绝对 路径
func GetMidDirByAbsPath(dir string, fileAbsPath string) string {
	var dirPath string
	absFilePath := fileAbsPath
	fileName := GetFileSuffix(fileAbsPath, "/")
	absDir, _ := filepath.Abs(dir)
	// fmt.Println("absFilePath: ", absFilePath)
	// fmt.Println("absDir: ", absDir)
	// fmt.Println("fileName: ", fileName)
	dirPath = absFilePath[:len(absFilePath)-len(fileName)]
	dirPath = dirPath[len(absDir):]
	// fmt.Println("---------dirPath------: ", dirPath)
	return dirPath
}

// 根据两个文件路径判断是否是同一个文件
func IsSameFile(a string, b string) bool {
	a, _ = filepath.Abs(a)
	b, _ = filepath.Abs(b)
	return a == b
}

func GetFileSuffix(file string, slip string) string {
	i := strings.LastIndex(file, slip)
	return file[i+1:]
}

func FileFilter(s string, filter mapset.Set) bool {
	return filter.Contains(s)
}

// 要发送到主机的IP、Port；当前共享的目录地址、要发送的文件名(绝对路径)
func SendFiles(addr string, port string, dir string, fileName string) error {
	defer func() {
		fmt.Println("End of client file sending function")
	}()

	dirAsb, err := filepath.Abs(dir)
	fmt.Println("dir: ", dirAsb, "name: ", fileName)
	file_name, err := filepath.Rel(dirAsb, fileName)
	fmt.Println("准备发送文件：", file_name)
	file_names := []string{file_name}

	addr, network := addr+":"+port, "tcp"
	tcpAddr, err := net.ResolveTCPAddr(network, addr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return err
	}

	conTcp, err := net.DialTCP(network, nil, tcpAddr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return err
	}
	println("发送文件建立的链接port:", conTcp.LocalAddr().String())
	tcpCon := server.NewTcp(conTcp)
	// dir = fileName[len(dirAsb):]
	// dir = dir[:strings.Index(dir, file_name)]
	// fmt.Println("更改后的dir为: ", dir)

	srv := NewClient(tcpCon, dir, file_names)
	_ = srv.SendFile()
	return nil
}

func SendRenameMsg(addr string, port string, old_path string, new_path string) {
	oldAndNewPath := old_path + "|" + new_path

	addr, network := addr+":"+port, "tcp"
	tcpAddr, err := net.ResolveTCPAddr(network, addr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return
	}

	conTcp, err := net.DialTCP(network, nil, tcpAddr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return
	}
	println("发送文件建立的链接port:", conTcp.LocalAddr().String())
	tcpCon := server.NewTcp(conTcp)

	srv := NewClient(tcpCon, "", nil)
	srv.GetConn().SendMsg(msg.NewRenameMsg(oldAndNewPath))
	time.Sleep(50 * time.Millisecond)
	fmt.Println("send rename tcp connect is closed ? ", srv.GetConn().IsClose())

}

func SendRemoveMsg(addr string, port string, fileName string) {

	addr, network := addr+":"+port, "tcp"
	tcpAddr, err := net.ResolveTCPAddr(network, addr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return
	}

	conTcp, err := net.DialTCP(network, nil, tcpAddr)
	if err != nil {
		erro.NetErrShowTcp(err)
		return
	}
	println("发送文件建立的链接port:", conTcp.LocalAddr().String())
	tcpCon := server.NewTcp(conTcp)

	srv := NewClient(tcpCon, "", nil)
	srv.GetConn().SendMsg(msg.NewRemoveMsg(fileName))
	time.Sleep(50 * time.Millisecond)
	fmt.Println("send rename tcp connect is closed ? ", srv.GetConn().IsClose())
}

// 根据绝对路径获取相对路径，第一个是基础判断的路径，第二个是要求的路径
func GetFileNameByAbs(base string, f string) string {
	fileName, err := filepath.Abs(f)
	dirAsb, err := filepath.Abs(base)
	erro.ComErrShow(err)
	// fmt.Println("dir: ", dirAsb, "name: ", fileName)
	file_name, err := filepath.Rel(dirAsb, fileName)
	erro.ComErrShow(err)
	return file_name
}
